<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Comment extends User_Controller {
	function __construct(){
		parent::__construct();
	}

	function add($data_id, $data_type){
		$this->smarty->assign("data_id", $data_id);
		$this->smarty->assign("data_type", $data_type);
		$this->smarty->display("comment/save.html");
	}

	function save(){
		if(empty($this->user_data['id'])){
			echo 9;exit;
		}
		$post_data = $this->input->post();
		$comment_data['uid'] 		= $this->user_data['id'];
		$comment_data['top_id'] 	= $post_data['data_id'];
		$comment_data['type'] 		= $post_data['data_type'];
		$comment_data['content'] 	= $post_data['content'];
		$comment_data['star'] 		= $post_data['star'];
		$comment_data['ip']			= getip();
		$comment_data['add_time']	= time();
		$insert_id = 0;
		if(in_array($post_data['data_type'], array(1,2,3))){
			$this->db->insert("course_comment", $comment_data);
			$insert_id = $this->db->insert_id();
			$this->db->where(array('id'=>$post_data['data_id']));
	  		$this->db->set('comment_count','comment_count + 1',FALSE);
	  		$this->db->set('star'.$post_data['star'], 'star'.$post_data['star'].' + 1',FALSE);
	  		$this->db->update('course');
		}else if($post_data['post_data'] == 4){
			$this->db->insert("tiku_comment", $comment_data);
			$insert_id = $this->db->insert_id();
		}else if($post_data['post_data'] == 5){
			$this->db->insert("shixun_comment", $comment_data);
			$insert_id = $this->db->insert_id();
		}
		if($insert_id > 0){
			echo 1;
		}else{
			echo 0;
		}
	}
}
